<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png">
        <span>电商管理系统</span>
      </div>
      <el-button
        type="primary"
        size="default"
        @click="logout"
      >退出</el-button></el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside
        :width="isCollapse ? '64px' : '200px'"
      >
        <div class="toggle-button" @click="changeCollapse">|||</div>
        <el-menu background-color="#222744" text-color="#fff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activeIndex">
          <el-submenu v-for="item in menuList" :key="item.id" :index="'/' + item.path">
            <template slot="title">
              <i :class="'iconfont ' + iconObj[item.id]" />
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item v-for="menuItem in item.children" :key="menuItem.id" :index="'/' + menuItem.path" @click="changeActiveIndex('/' + menuItem.path)">
              <i :class="'iconfont ' + menuItemIconObj[menuItem.id]" /> <span>{{ menuItem.authName }}</span></el-menu-item>
          </el-submenu>
        </el-menu></el-aside>
      <!-- 内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      // 侧边栏是否折叠
      isCollapse: false,
      activeIndex: '',
      iconObj: {
        '125': 'icon-account-fill',
        '103': 'icon-form',
        '101': 'icon-searchcart-fill',
        '102': 'icon-order',
        '145': 'icon-tradingdata'
      },
      menuItemIconObj: {
        '110': 'icon-certified-supplier-fill',
        '111': 'icon-add-account1',
        '112': 'icon-all',
        '104': 'icon-form',
        '115': 'icon-Householdappliances',
        '121': 'icon-Similarproducts-fill',
        '107': 'icon-category',
        '146': 'icon-form-fill'
      }
    }
  },
  created() {
    this.activeIndex = window.sessionStorage.getItem('activeIndex')
    this.getMenuList()
  },
  methods: {
    logout() {
      window.sessionStorage.clear('token')
      this.$router.push('/login')
    },
    // 获取侧边栏菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.menuList = res.data
      // console.log(res)
      // this.menuList.forEach(e => {
      //   e.children.forEach(d => {
      //     // console.log(d)
      //     console.log(d.authName, '+', d.id)
      //   })
      // })
    },
    changeCollapse() {
      this.isCollapse = !this.isCollapse
    },
    changeActiveIndex(activeIndexData) {
      this.activeIndex = activeIndexData
      window.sessionStorage.setItem('activeIndex', activeIndexData)
    }
  }
}
</script>

<style lang="less">
.is-vertical {
  height: 100%;

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;

    > div {
      font-size: 24px;
      color: #fff;
      font-weight: bold;
      display: flex;
      align-items: center;

      img {
        width: 52px;
        height: auto;
      }

      span {
        margin-left: 18px;
      }
    }
  }

  .el-container {
    .el-aside {
      background-color: #222744;

      .toggle-button {
        background-color: #4A5064;
        text-align: center;
        line-height: 24px;
        font-size: 10px;
        letter-spacing: .3em;
        color: #fff;
        cursor: pointer;
      }

      .el-menu {
        border-right: none;
      }
    }

    .el-main {
      background-color: #eaedf1;
    }
  }
}
</style>
